package cn.tedu._07springaop.egaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CartAspect {
//    @Before(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void beforeMethod(){
//        System.out.println("开始添加购物车");
//    }
//
//    @After(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
//    public void afterMethod(){
//        System.out.println("操作完成");
//    }
//
//    @AfterReturning(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))",returning = "r")
//    public void afterReturning(Boolean r){
//        if(r){
//            System.out.println("添加购物车成功");
//        }else {
//            System.out.println("添加购物车失败");
//        }
//    }

    @Around(value = "execution(public boolean cn.tedu._07springaop.egaop.CartServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint joinPoint){
        boolean result = false;

        try {
            System.out.println("开始添加购物车");
            result = (boolean) joinPoint.proceed();
            if(result){
                System.out.println("添加购物车成功");
            }else {
                System.out.println("添加购物车失败");
            }
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }finally {
            System.out.println("操作完成");
        }
        return result;
    }

}
